package com.foreveross.crawl.application.impl.computer;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.InetAddress;
import java.net.NetworkInterface;
import java.net.SocketException;
import java.net.UnknownHostException;
import java.util.Enumeration;
import java.util.StringTokenizer;

import com.foreveross.crawl.application.computer.AbstratComputerInfo;
import com.foreveross.crawl.application.computer.IComputerInfoApplication;

public class ComputerInfoByLinuxAppicationImpl extends AbstratComputerInfo
		implements IComputerInfoApplication {

	@Override
	public void initInetAddress() throws Exception {
		Enumeration<NetworkInterface> netInterfaces = (Enumeration<NetworkInterface>) NetworkInterface
				.getNetworkInterfaces();
		boolean flag = false;
		InetAddress address = null;
		while (netInterfaces.hasMoreElements()) {
			if (flag) {
				break;
			}
			NetworkInterface net = netInterfaces.nextElement();
			Enumeration<InetAddress> ips = net.getInetAddresses();
			while (ips.hasMoreElements()) {
				address = (InetAddress) ips.nextElement();
				if (address.isSiteLocalAddress()
						&& !address.isLoopbackAddress() // 127.开头的都是lookback地址
						&& address.getHostAddress().indexOf(":") == -1) {
					flag = true;
					break;
				}
			}
			if (address != null) {
				super.address = address;
			} else {
				throw new Exception("InetAddress is null!");
			}
		}
	}

	@Override
	public double getCpuRate() {
		InputStream is = null;
		InputStreamReader isr = null;
		BufferedReader brStat = null;
		StringTokenizer tokenStat = null;
		String osVersion = System.getProperties().getProperty("os.version");
		try {
			Process process = Runtime.getRuntime().exec("top -b -n 1");
			is = process.getInputStream();
			isr = new InputStreamReader(is);
			brStat = new BufferedReader(isr);

			if (osVersion.equals("2.4")) {
				brStat.readLine();
				brStat.readLine();
				brStat.readLine();
				brStat.readLine();

				tokenStat = new StringTokenizer(brStat.readLine());
				tokenStat.nextToken();
				tokenStat.nextToken();
				String user = tokenStat.nextToken();
				tokenStat.nextToken();
				String system = tokenStat.nextToken();
				tokenStat.nextToken();
				String nice = tokenStat.nextToken();

				System.out.println(user + " , " + system + " , " + nice);

				user = user.substring(0, user.indexOf("%"));
				system = system.substring(0, system.indexOf("%"));
				nice = nice.substring(0, nice.indexOf("%"));

				float userUsage = new Float(user).floatValue();
				float systemUsage = new Float(system).floatValue();
				float niceUsage = new Float(nice).floatValue();

				return (userUsage + systemUsage + niceUsage) / 100;
			} else {
				brStat.readLine();
				brStat.readLine();

				tokenStat = new StringTokenizer(brStat.readLine());
				tokenStat.nextToken();
				tokenStat.nextToken();
				tokenStat.nextToken();
				tokenStat.nextToken();
				tokenStat.nextToken();
				tokenStat.nextToken();
				tokenStat.nextToken();
				String cpuUsage = tokenStat.nextToken();

				System.out.println("CPU idle : " + cpuUsage);
				Float usage = new Float(cpuUsage.substring(0,
						cpuUsage.indexOf("%")));

				return (1 - usage.floatValue() / 100);
			}

		} catch (IOException ioe) {
			System.out.println(ioe.getMessage());
			freeResource(is, isr, brStat);
			return 1;
		} finally {
			freeResource(is, isr, brStat);
		}
	}

	private static void freeResource(InputStream is, InputStreamReader isr,
			BufferedReader br) {
		try {
			if (is != null)
				is.close();
			if (isr != null)
				isr.close();
			if (br != null)
				br.close();
		} catch (IOException ioe) {
			System.out.println(ioe.getMessage());
		}
	}

}
